﻿$import( "SmitearWebGL.Core.ResourceItem" );
$import( "SmitearWebGL.Resources.Texture" );

SmitearWebGL.Resources.RenderSurface = SmitearWebGL.Core.ResourceItem.extend(
{
	/******** constructor ********/

	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Resources.RenderSurface" );

		this._glFrameBuffer = null;
		this._glDepthBuffer = null;
	}
	,
	createDepthSurface : function( width, height )
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );

		if ( ! this._glDepthBuffer ) this._glDepthBuffer = this.context.gl.createRenderbuffer();
		else this.clearDepthSurface();
		
		this.bind();
		this.context.gl.bindRenderbuffer( this.context.gl.RENDERBUFFER, this._glDepthBuffer );
		this.context.gl.renderbufferStorage( this.context.gl.RENDERBUFFER, this.context.gl.DEPTH_STENCIL, width, height );
		this.context.gl.framebufferRenderbuffer( this.context.gl.FRAMEBUFFER, this.context.gl.DEPTH_STENCIL_ATTACHMENT, this.context.gl.RENDERBUFFER, this._glDepthBuffer );
		this.context.gl.bindRenderbuffer( this.context.gl.RENDERBUFFER, null );
		
		return true;
	}
	,
	bindRenderTarget : function( texture, level )
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		if ( ! texture )
			this.clearRenderTarget();
		else
			this.context.gl.framebufferTexture2D( this.context.gl.FRAMEBUFFER, this.context.gl.COLOR_ATTACHMENT0, this.context.gl.TEXTURE_2D, texture._glTexture, level );
		
		return true;
	}
	,
	clearRenderTarget : function()
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );

		this.context.gl.framebufferTexture2D( this.context.gl.FRAMEBUFFER, this.context.gl.COLOR_ATTACHMENT0, this.context.gl.TEXTURE_2D, null, 0 );
		
		return true;
	}
	,
	clearDepthSurface : function()
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );

		this.context.gl.framebufferRenderbuffer( this.context.gl.FRAMEBUFFER, this.context.gl.DEPTH_STENCIL_ATTACHMENT, this.context.gl.RENDERBUFFER, null );
		this.context.gl.deleteRenderbuffer( this._glDepthBuffer );
		this._glDepthBuffer = null;
		
		return true;
	}
	,
	isBinding : function()
	{
		return SmitearWebGL.Resources.RenderSurface.BindingSurface == this;
	}
	,
	bind : function()
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );

		if ( SmitearWebGL.Resources.RenderSurface.BindingSurface )
		{
			if ( SmitearWebGL.Resources.RenderSurface.BindingSurface == this )
				return;
			else
			{
				$THROW( false, "渲染表面被绑定到别的实例中,必须先解除绑定" );
				return;
			}
		}
		else
		{
			this.context.gl.bindFramebuffer( this.context.gl.FRAMEBUFFER, this._glFrameBuffer );
			SmitearWebGL.Resources.RenderSurface.BindingSurface = this;
		}
	}
	,
	unbind : function()
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		if ( ! SmitearWebGL.Resources.RenderSurface.BindingSurface ) return;

		if ( SmitearWebGL.Resources.RenderSurface.BindingSurface == this )
		{
			this.context.gl.bindFramebuffer( this.context.gl.FRAMEBUFFER, null );
			SmitearWebGL.Resources.RenderSurface.BindingSurface = null;
		}
		else
		{
			$THROW( false, "被绑定的渲染表面不是此实例" );
		}
	}
	,

	/******** inherit method ********/

	_createResource : function()
	{
		this._super();
		
		this._glFrameBuffer = this.context.gl.createFramebuffer();
	}
	,
	_destroyResource : function()
	{
		this._super();
		
		if ( this._glFrameBuffer )
			this.context.gl.deleteFramebuffer( this._glFrameBuffer );
			
		if ( this._glDepthBuffer )
			this.context.gl.deleteRenderbuffer( this._glDepthBuffer );

		this._glFrameBuffer = null;
		this._glDepthBuffer = null;
	}
}
);

SmitearWebGL.Resources.RenderSurface.BindingSurface = null;